{extend name='common/common' /}

{block name="style"}
<style type="text/css">
    .container-fluid .btn.btn-sm{
        float: right;
        margin-left: 10px;
    }
    .container-fluid{
        padding-right: 0px;
    }
    tr{
        cursor: pointer;
    }
    .layui-form-item{
        margin-bottom: 0px;
    }
    .layui-form{
        display: inline-block;
    }
    .ChartTips{
        position: absolute;
        width: 100%;
        height: 470px;
        z-index: 100;
        background-color: #fff;
        text-align: center;
        line-height: 470px;
        font-size: 20px;
    }
</style>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>订单统计</h5>
        </div>
        <div class="ibox-content">
            <!-- 新增时间  -->
            <div class="form-group" style="float: left">
                <label>日期范围：</label>
                <div class="layui-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" name="timeInterval" id="timeInterval" placeholder="开始 到 结束 默认为一周">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group" style="float: left">
                <label>时长：</label>
                <div class="layui-form-item" style="display: inline-block">
                    <input type="number" class="form-control" id="timeLength" name="timeLength" placeholder="默认时长一天">
                </div>
            </div>
            <a class="btn btn-primary btn-sm getIDs" id="statiSEE" style="margin-left: 20px">查  看</a>
            <div class="clear"></div>
            <div class="ChartTips">
                图表加载中:Loading...
            </div>
            <div id="main1" style="width: 100%;height:450px; min-width:400px;">

            </div>
        </div>
        <div class="ibox-content">
            <!--搜索框开始 pull-right-->
            <form id='commentForm' role="form" method="post" class="form-inline pull-left">
                <div class="content clearfix m-b">
                    <div class="form-group">
                        <label>部门：</label>
                        <input type="text" class="form-control" id="depart" name="depart" readonly placeholder="请选择部门">
                        <input type="hidden" class="form-control" id="depart_id" name="depart_id" readonly>
                    </div>
                    <div class="form-group">
                        <label>咨询师姓名：</label>
                        <input type="text" class="form-control" id="staff" name="staff">
                    </div>

                    <div class="form-group">
                        <label>姓名：</label>
                        <input type="text" class="form-control" id="stu_name" name="name">
                    </div>
                    <!-- 新增时间  -->
                    <div class="form-group">
                        <label>日期范围：</label>
                        <div class="layui-form">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" name="KL-time" id="KL-time" placeholder="开始 到 结束">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary" type="button" style="margin-top:5px" id="search"><strong>搜 索</strong>
                        </button>
                    </div>
					<!--  日/周/月/季/年   -->
					<div class="form-group">
						<button class="btn btn-default reportBtns" type="button" style="margin-top:5px" id="seaTimeDate"><strong>日 报</strong></button>
					</div>
					<div class="form-group">
						<button class="btn btn-success reportBtns" type="button" style="margin-top:5px" id="seaTimeWeek"><strong>周 报</strong></button>
					</div>
					<div class="form-group">
						<button class="btn btn-success reportBtns" type="button" style="margin-top:5px" id="seaTimeMonth"><strong>月 报</strong></button>
					</div>
					<div class="form-group">
						<button class="btn btn-success reportBtns" type="button" style="margin-top:5px" id="seaTimeQuarter"><strong>季 报</strong></button>
					</div>
					<div class="form-group">
						<button class="btn btn-success reportBtns" type="button" style="margin-top:5px" id="seaTimeYear"><strong>年 报</strong></button>
					</div>

                </div>
            </form>
            <!--搜索框结束-->

            <div class="example-wrap">
                <div class="example">
                    <table id="cusTable">
                        <thead>
                        <th class="text-center" data-field="id">订单ID</th>
                        <th class="text-center" data-field="order_sn">订单号</th>
                        <th class="text-center" data-field="c_time">创建时间</th>
                        <th class="text-center" data-field="stu_name">姓名</th>
                        <th class="text-center" data-field="phone">联系电话</th>
                        <th class="text-center" data-field="school">报考学院</th>
                        <th class="text-center" data-field="major">专业</th>
                        <th class="text-center" data-field="gradeN">层次</th>
                        <th class="text-center" data-field="payment_methodN">缴费方式</th>
                        <th class="text-center" data-field="consultantN">所属咨询师</th>
                        <th class="text-center" data-field="total_amount">总金额(元)</th>
                        </thead>
                    </table>
                </div>
            </div>
            <!-- End Example Pagination -->
        </div>
    </div>
</div>
<!-- End Panel Other -->
<div id="remarks" style="display: none;padding: 15px;"></div>
<div class="laycontent" style="display: none;width: 300px;padding: 30px 15px;">
    <ul id="tree"></ul>
</div>
{/block}
{block name="script"}
<script src="__JS__/bootstrap-table-export.js"></script>
<script src="__JS__/tableExport.js"></script>

<script>
    $(function(){
        getTree();
        layui.use(['form'], function(){
            var form = layui.form;
        });

        $('#depart').on('click',function(){
            selectSchoolBox = layer.open({
                type: 1,
                title: '请选择部门',
                anim: 2,
                skin: 'layui-layer-molv', //加上边框
                area: ['300px', '350px'], //宽高
                content: $(".laycontent")
            });
        });
    });
    function getTree(){
        layui.use(['tree', 'layer'], function(){
            var layer = layui.layer;

            $.getJSON("{:url('Department/index')}", function(res){
                if(111 == res.code){
                    window.location.reload();
                }
                layui.tree({
                    elem: '#tree'
                    ,nodes: res.data
                    ,click: function(node){
                        $('#depart').val(node.name);
                        $('#depart_id').val(node.id);
                        layer.closeAll();
                    }
                });
            });
        });
    }
</script>
<script type="text/javascript">
    //统计
    function statis() {
        var myChart = echarts.init(document.getElementById('main1'));
        // 指定图表的配置项和数据
        //ajax请求数据
        var timeInterval = $('#timeInterval').val();
        var timeLength = $('#timeLength').val();
        var pageNumber = 1;
        var pageSize = 1000;

        var stu_name = $('#stu_name').val();

        var staff = $('#staff').val();//咨询师名字
        var depart_id = $('#depart_id').val();//部门id

        if(timeLength < 0){
            layer.msg("时长不能小于0");
            return false;
        }
        $(".ChartTips").show();
        $.post("{:url('statistics/ajaxBusinStatis')}",{
            timeInterval:timeInterval,
            timeLength:timeLength,

            pageNumber:pageNumber,
            pageSize:pageSize,

            stu_name:stu_name,
            staff:staff,
            depart_id:depart_id
        },function (res) {
            console.log(res.kingData.data);
            if(res.code == 1){
                var option = {
                    title: {
                        text: '业务统计图'
                    },
                    tooltip : {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#6a7985'
                            }
                        }
                    },
                    legend: {
                        //线条指示
                        data:[res.kingData.name]
                    },
                    toolbox: {
                        //工具栏
                        feature: {
                            saveAsImage: {},
                            magicType: {},
                            restore: {},
                            dataView: {},
                            dataZoom: {}
                        }
                    },
                    grid: {
                        //直角坐标系内绘图网格
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis : [
                        {
                            type : 'category',
                            boundaryGap : false,
                            data : res.Xdata
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value'
                        }
                    ],
                    series : [
                        {
                            name:res.kingData.name,
                            type:'line',
                            stack: '总量',
                            areaStyle: {normal: {}},
                            data:res.kingData.data,
                        },
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
                $(".ChartTips").hide();
                layer.msg(res.Msg);
            }else{
                layer.msg('图表加载失败');
            }
        })
    }
    function DoOnMsoNumberFormat(cell, row, col) {
        var result = "";
        if (row > 0 && col == 0)
            result = "\\@";
        return result;
    }
    function initTable() {
        //先销毁表格
        $('#cusTable').bootstrapTable('destroy');
        //初始化表格,动态从服务器加载数据
        $("#cusTable").bootstrapTable({
            method: "get",  //使用get请求到服务器获取数据
            url: "{:url('Statistics/businStatis')}", //获取数据的地址
            striped: true,  //表格显示条纹
            pagination: true, //启动分页
            pageSize: 20,  //每页显示的记录数
            pageNumber:1, //当前第几页
            pageList: [25, 50, 100, 150, 200],  //记录数可选列表
            sidePagination: "server", //表示服务端请求
            paginationFirstText: "首页",
            paginationPreText: "上一页",
            paginationNextText: "下一页",
            paginationLastText: "尾页",
            queryParamsType : "undefined",
            showToggle:true,
            showColumns:true,
            showExport: true,  //是否显示导出按钮
            exportTypes:['excel'],  //导出文件类型
            Icons:'glyphicon-export',
            exportOptions:{
                // ignoreColumn: [0,1],  //忽略某一列的索引
                fileName: '订单统计',  //文件名称设置
                worksheetName: 'sheet1',  //表格工作区名称
                tableName: '订单统计报表',
                excelstyles: ['background-color', 'color', 'font-size', 'font-weight'],
                onMsoNumberFormat: DoOnMsoNumberFormat
            },
            queryParams: function queryParams(params) {   //设置查询参数
                var param = {
                    pageNumber: params.pageNumber,
                    pageSize: params.pageSize,
                    stu_name:$('#stu_name').val(),
                    c_time:$('#KL-time').val(),
                    staff:$('#staff').val(),//咨询师名字
                    depart_id:$('#depart_id').val(),//部门id
                };
                return param;
            },
            onLoadSuccess: function(res){ //加载成功时执行
                if(111 == res.code){
                    window.location.reload();
                }
                layer.msg("加载数据成功", {time : 1000});
                var tr = '<tr><td colspan="10" class="text-right span-strong">总计</td>'
                        + '<td class="text-center span-strong">' + res.sum + '</td></tr>';
                $("#cusTable tbody").append(tr);
            },
            onLoadError: function(){  //加载失败时执行
                layer.msg("加载数据失败");
            }
        });
    }

    $(document).ready(function () {
        //调用函数，初始化表格
        initTable();
        //调用函数，初始化统计图
        statis();
        //当点击查询按钮的时候执行
        $("#search").bind("click", initTable);
        //当点击查询按钮的时候执行
        $("#statiSEE").bind("click", function(){statis();});

    });

    //时间选择
    layui.use('laydate', function() {
        var laydate = layui.laydate;
        //日期范围
        laydate.render({
            elem: '#KL-time'
            ,range: true
        });
        laydate.render({
            elem: '#timeInterval'
            ,range: true
        });
    })
	// 日/周/月/季/年报表
	$(function() {
		var seaTime = '';
		layui.use(['form','laydate'], function() {
			var form = layui.form,laydate = layui.laydate;
			laydate.render({elem: '#KL-time',range: true});
			laydate.render({elem: '#timeInterval',range: true});
		});
		//当点击查询按钮的时候执行

		$("#timeInterval").on("click",function(){
			statis();
		});

		$(".reportBtns").on('click',function(){
			$(".reportBtns").addClass('btn-success').removeClass('btn-default');
			$(this).addClass('btn-default');
		});
		//日报
		$("#seaTimeDate").on('click',function(){
			if(seaTime != ""){
				seaTime = "";
				$("#KL-time").val("");
				initTable();
			}
		});
		//周报
		$("#seaTimeWeek").on('click',function(){
			var week = getWeekStartDate() + " - " + getWeekEndDate();
			if(week != seaTime){
				seaTime = week;
				$("#KL-time").val(week);
				initTable();
			}
		});
		//月报
		$("#seaTimeMonth").on('click',function(){
			var month = getMonthStartDate() + " - " + getMonthEndDate();
			if(month != seaTime){
				seaTime = month;
				$("#KL-time").val(month);
				initTable();
			}
		});
		//季报
		$("#seaTimeQuarter").on('click',function(){
			var quarter = getQuarterStartDate() + " - " + getQuarterEndDate();
			if(quarter != seaTime){
				seaTime = quarter;
				$("#KL-time").val(quarter);
				initTable();
			}
		});
		//年报
		$("#seaTimeYear").on('click',function(){
			var year = new Date().getYear();
			year += (year < 2000) ? 1900 : 0;
			year = year + "-01-01 - " + year + "-12-31";
			if(year != seaTime){
				seaTime = year;
				$("#KL-time").val(year);
				initTable();
			}
		});
	});
</script>
{/block}
